package org.jsola.hr.web;

import lombok.extern.slf4j.Slf4j;
import org.jsola.admin.vo.SiteListVO;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.PhoneVerificationCodeDTO;
import org.jsola.hr.dto.RecruitXcxLoginByPhoneDTO;
import org.jsola.hr.dto.RecruitXcxLoginDTO;
import org.jsola.hr.dto.WeChatBindFromOffAccountDTO;
import org.jsola.hr.provider.IMessageProviderService;
import org.jsola.hr.provider.IRecruitProviderService;
import org.jsola.hr.provider.IUserProviderService;
import org.jsola.hr.service.IEmpInfoService;
import org.jsola.hr.vo.EmpAllInfoVO;
import org.jsola.hr.vo.EmpLoginResultVO;
import org.jsola.hr.vo.RecruitResultVO;
import org.jsola.hr.vo.WeChatSiteBindVO;
import org.jsola.message.dto.ShortMessageDTO;
import org.jsola.message.service.ISmsSendService;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.jsola.user.dto.RecruitWechatXcxLoginDTO;
import org.jsola.user.dto.login.WechatWebLoginDTO;
import org.jsola.user.dto.login.WechatXcxLoginDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static org.jsola.hr.constant.HrConstants.USER_CERTIFICATION;

/**
 * @author JanusMix
 */
@Slf4j
@RestController("hrLoginController")
@RequestMapping(HrConstants.API_PATH + "/v1/login")
public class LoginController implements ILoginControllerAPI {

    @Autowired
    private IUserProviderService userProviderService;

    @Autowired
    private IEmpInfoService empInfoService;

    @Autowired
    private IRecruitProviderService recruitProviderService;


    @Autowired
    private ISmsSendService sendShortMessage;

    @Value("${spring.application.name}")
    private String appName;


    @Override
    @PostMapping(value = "/wechat/xcx")
    public Result<EmpLoginResultVO> wechatXcxLogin(@Valid @RequestBody WechatXcxLoginDTO wechatXcxLoginDTO) {
        return Result.success(userProviderService.wechatXcxLogin(wechatXcxLoginDTO));
    }

    @Override
    @PostMapping(value = "/wechat/xcxForAdmin")
    public Result<EmpLoginResultVO> wechatXcxLoginForAdmin(@Valid @RequestBody WechatXcxLoginDTO wechatXcxLoginDTO) {
        return Result.success(userProviderService.wechatXcxLoginForAdmin(wechatXcxLoginDTO));
    }

    @Override
    @PostMapping(value = "/wechat/getPhone")
    public Result<String> getPhone(@Valid @RequestBody WechatXcxLoginDTO wechatXcxLoginDTO) {
        return Result.success(
                recruitProviderService.getPhone(wechatXcxLoginDTO)
        );
    }

    @Override
    @PostMapping(value = "/wechat/getPhone2/{unionId}")
    public Result<RecruitResultVO> getPhone2(@Valid @RequestBody RecruitXcxLoginDTO recruitXcxLoginDTO
            , @PathVariable String unionId) {
        return Result.success(
                recruitProviderService.getPhone2(recruitXcxLoginDTO, unionId)
        );
    }

    @Override
    @PostMapping(value = "/wechat/getPhoneAndCaptcha/{unionId}")
    public Result<RecruitResultVO> getPhoneAndCaptcha(@Valid @RequestBody RecruitXcxLoginByPhoneDTO recruitXcxLoginByPhoneDTO
            , @PathVariable String unionId) {
        return Result.success(
                recruitProviderService.getPhoneAndCaptcha(recruitXcxLoginByPhoneDTO, unionId)
        );
    }

    @Override
    @PostMapping(value = "/wechat/recruit")
    public Result<RecruitResultVO> wechatRecruitLogin(@Valid @RequestBody RecruitWechatXcxLoginDTO recruitWechatXcxLoginDTO) {
        return Result.success(
                recruitProviderService.wechatRecruitLogin(recruitWechatXcxLoginDTO)
        );
    }

    @Override
    @PostMapping(value = "/wechat/recruit2")
    public Result<RecruitResultVO> wechatRecruitLogin2(@Valid @RequestBody RecruitWechatXcxLoginDTO recruitWechatXcxLoginDTO) {
        return Result.success(
                recruitProviderService.wechatRecruitLogin2(recruitWechatXcxLoginDTO)
        );
    }

    @Override
    @PostMapping(value = "/wechat/web")
    public Result<EmpLoginResultVO> wechatWebLogin(@Valid @RequestBody WechatWebLoginDTO wechatWebLoginDTO) {
        return Result.success(userProviderService.wechatWebLogin(wechatWebLoginDTO));
    }

    @Override
    @GetMapping(value = "/getEmpAllInfoByToken")
    public Result<EmpAllInfoVO> getEmpAllInfoByToken() {
        TokenUser tokenUser = UserContext.getUser();
        ParamException.notNull(tokenUser, "未获取到登录信息，请刷新后再试");
        // todo 查看员工考勤月统计归档权限校验


        return Result.success(
                empInfoService.findEmpInfoRelationByUserId(tokenUser.getUserId(), tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/getEmpAllInfoByToken2")
    public Result<EmpAllInfoVO> getEmpAllInfoByToken2() {
        TokenUser tokenUser = UserContext.getUser();
        ParamException.notNull(tokenUser, "未获取到登录信息，请刷新后再试");
        // todo 查看员工考勤月统计归档权限校验


        return Result.success(
                empInfoService.findEmpInfoRelationByUserId2(tokenUser.getUserId(), tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/getSiteIdListByPhone")
    public Result<List<String>> getSiteIdListByPhone(String phone) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工考勤月统计归档权限校验


        return Result.success(
                empInfoService.getSiteIdListByPhone(phone, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/getSiteListVOByPhone")
    public Result<List<SiteListVO>> getSiteListVOByPhone(String phone) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工考勤月统计归档权限校验


        return Result.success(
                empInfoService.getSiteListVOByPhone(phone, tokenUser)
        );
    }

    @Override
    @PostMapping(value = "/bindWeChatFromOfficialAccount")
    public Result<Boolean> bindWeChatFromOfficialAccount(@Valid @RequestBody WeChatBindFromOffAccountDTO weChatBindFromOffAccountDTO) {
        log.info("======接收到unionId：" + weChatBindFromOffAccountDTO.getUnionId());
        return Result.success(userProviderService.bindWeChatFromOfficialAccount(weChatBindFromOffAccountDTO));
    }

    @Override
    @GetMapping(value = "/checkBindByUnionId")
    public Result<WeChatSiteBindVO> checkBindByUnionId(String unionId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工考勤月统计归档权限校验
        log.info("======获取用户企业绑定的unionId参数：" + unionId);

        return Result.success(
                empInfoService.checkBindByUnionId(unionId, tokenUser)
        );
    }
    @Override
    @PostMapping(value = "/verifyCaptcha")
    public Result<String> verifyCaptcha(@RequestBody @Valid PhoneVerificationCodeDTO phoneVerificationCodeDTO) {
        TokenUser tokenUser = UserContext.getUser();
        if ("18500000000".equals(phoneVerificationCodeDTO.getPhone()) && "4444".equals(phoneVerificationCodeDTO.getCode())) {
            return Result.success();
        }

        //校验短信验证码
        ShortMessageDTO shortMessageDTO = new ShortMessageDTO();
        shortMessageDTO.setPhone(phoneVerificationCodeDTO.getPhone());
        shortMessageDTO.setScene(USER_CERTIFICATION);
        shortMessageDTO.setCaptcha(phoneVerificationCodeDTO.getCode());
        shortMessageDTO.setAppName(appName);
        boolean checkCode = sendShortMessage.verifyCaptcha(shortMessageDTO, tokenUser);
        if (!checkCode) {
            throw new ParamException("验证码不正确");
        }
        return Result.success();

    }
}
